package com.wln.sms.sms.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.rabbitmq.client.Channel;
import com.wln.sms.sms.dao.GoodsActivityDao;
import com.wln.sms.sms.entity.GoodsActivityEntity;
import com.wln.sms.sms.entity.OrderInfoEntity;
import com.wln.sms.sms.feign.GoodsFeignService;
import com.wln.sms.sms.service.OrderInfoService;
import com.wln.sms.sms.util.IdWorker;
import com.wln.sms.sms.vo.ActivityGoodsVo;
import com.wln.sms.sms.vo.AddOrderVo;
import com.wln.sms.sms.vo.GoodsVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Component
@RabbitListener(queues = "order")
public class OrderMqConfig {

    @Autowired
    private RedisTemplate redisTemplate;
    @SuppressWarnings("all")
    @Autowired
    private GoodsActivityDao goodsActivityDao;

    @Autowired
    private IdWorker idWorker;

    @RabbitHandler
    public void addorder(String s, Message message, Channel channel) throws IOException {

        ActivityGoodsVo activityGoodsVo = JSON.parseObject(s, new TypeReference<ActivityGoodsVo>() {
        });
        BoundHashOperations goods = redisTemplate.boundHashOps("goods");

        Integer o = (Integer) goods.get(activityGoodsVo.getGoodsId());

        if(o>0){

            goods.put(activityGoodsVo.getGoodsId(),o-activityGoodsVo.getGoodsCount());
        }

        BoundHashOperations activitylist = redisTemplate.boundHashOps("activitylist");

        GoodsActivityEntity o1 = (GoodsActivityEntity) activitylist.get(activityGoodsVo.getActivityId());

        if(o1.getStockCount()>0){

            o1.setStockCount(o1.getStockCount()-activityGoodsVo.getGoodsCount());

            activitylist.put(activityGoodsVo.getActivityId(),o1);
        }

//        OrderInfoEntity infoEntity = new OrderInfoEntity();
//
//        infoEntity.setOorderId(idWorker.nextId());
//
//        infoEntity.setGoodsAddrId(12l);
//
//        infoEntity.setActivityId(activityGoodsVo.getActivityId());
//
//        infoEntity.setStatus(0);
//
//        infoEntity.setGoodsId(activityGoodsVo.getGoodsId());
//
//        infoEntity.setUserId(activityGoodsVo.getUserId());
//
//        infoEntity.setGoodsCount(activityGoodsVo.getGoodsCount());
//
//        infoEntity.setGoodsPrice(activityGoodsVo.getActivityPrices());
//
//        infoEntity.setGoodsName(activityGoodsVo.getGoodsName());
//
//        infoEntity.setGoodsChannel(1);
//
//        infoEntity.setCreateDate(new Date());
//
//        BoundHashOperations orderinfo = redisTemplate.boundHashOps("orderinfo");

       // orderinfo.put(infoEntity.getOorderId(),infoEntity);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
